#!/usr/bin/env python
# -*- coding: utf-8 -*-

import itertools

class RichCharStyle:
	
	itsSize = 12
	
	isBold = False
	isItalic = False
	isUnderlined = False
	isSuperscript = False
	isSubscript = False
	isSmallcap = False
	isAllcap = False
	isStruckout = False
	
	itsColour = (0, 0, 0)
	itsBGColour = (255, 255, 255)
	
	def __init__(self):
		return
	
	def Get(self):
		temp = RichCharStyle()
		temp.itsSize = self.itsSize
		temp.isBold = self.isBold
		temp.isItalic = self.isItalic
		temp.isUnderlined = self.isUnderlined
		temp.isSuperscript = self.isSuperscript
		temp.isSubscript = self.isSubscript
		temp.isSmallcap = self.isSmallcap
		temp.isAllcap = self.isAllcap
		temp.isStruckout = self.isStruckout
		temp.itsColour = self.isColour
		temp.itsBGColour = self.itsBGColour
		return temp
	
	def Set(self, other):
		self.itsSize = other.itsSize
		self.isBold = other.isBold
		self.isItalic = other.isItalic
		self.isUnderlined = other.isUnderlined
		self.isSuperscript = other.isSuperscript
		self.isSubscript = other.isSubscript
		self.isSmallcap = other.isSmallcap
		self.isAllcap = other.isAllcap
		self.isStruckout = other.isStruckout
		self.itsColour = other.itsColour
		self.itsBGColour = other.itsBGColour
		return True
	
	def __str__(self):
		output = ''
		output += str(self.itsSize)
		output += ' '
		if self.isBold: output += 'b'
		if self.isItalic: output += 'i'
		if self.isUnderlined: output += 'u'
		if self.isSuperscript: output += '^'
		if self.isSubscript: output += '_'
		if self.isSmallcap: output += 's'
		if self.isAllcap: output += 'S'
		if self.isStruckout: output += '-'
		output += ' '
		output += str(self.itsColour[0]) + '.' + str(self.itsColour[1]) + '.' + str(self.itsColour[2])
		output += ' '
		output += str(self.itsBGColour[0]) + '.' + str(self.itsBGColour[1]) + '.' + str(self.itsBGColour[2])
		output = '<' + output + '>'
		return output

class RichString:
	
	def __init__(self, text=''):
		
		self.itsChars = []
		self.itsStyles = []
		self.itsRoles = []
		
		self.itsFocusStyle = RichCharStyle()
		self.itsFocusRole = ''
		
		for char in text:
			self.itsChars.append(char)
			self.itsStyles.append(self.itsFocusStyle)
			self.itsRoles.append(self.itsFocusRole)
		
		return
	
	def __len__(self):
		return len(self.itsChars)
	
	def __str__(self):
		return ''.join(self.itsChars)
	
	def __getitem__(self, key):
		
		if key >= len(self.itsChars):
			raise IndexError("that's enough!")
		
		tRichString = RichString()
		tRichString.itsChars.append(self.itsChars[key])
		tRichString.itsStyles.append(self.itsStyles[key])
		tRichString.itsRoles.append(self.itsRoles[key])
		
		return tRichString
	
	def __addi__(self, other):
		
		tOutput = ''
		
		if isinstance(other, RichString):
			tRichString = RichString()
			for rchar in self:
				tRichString.itsChars.append(rchar.itsChars[0])
				tRichString.itsStyles.append(rchar.itsStyles[0])
				tRichString.itsRoles.append(rchar.itsRoles[0])
			for rchar in other:
				tRichString.itsChars.append(rchar.itsChars[0])
				tRichString.itsStyles.append(rchar.itsStyles[0])
				tRichString.itsRoles.append(rchar.itsRoles[0])
			tOutput = tRichString
		elif isinstance(other, str):
			for char in other:
				tRichString = RichString()
				tRichString.itsChars.append(char)
				tRichString.itsStyles.append(self.itsFocusStyle)
				tRichString.itsRoles.append(self.itsFocusRole)
				tOutput = self.__add__(tRichString)
		else:
			for char in str(other):
				tRichString = RichString()
				tRichString.itsChars.append(char)
				tRichString.itsStyles.append(self.itsFocusStyle)
				tRichString.itsRoles.append(self.itsFocusRole)
				tOutput = self.__add__(tRichString)
		
		return tOutput
	
	def Append(self, other):
		#print('RichString.Append(self, '+repr(other)+')')
		if isinstance(other, RichString):
			for rchar in other:
				self.itsChars.append(rchar.itsChars[0])
				self.itsStyles.append(rchar.itsStyles[0])
				self.itsRoles.append(rchar.itsRoles[0])
		elif isinstance(other, str):
			self.itsChars.extend(list(other))
			self.itsStyles.extend(itertools.repeat(self.itsFocusStyle, len(other)))
			self.itsRoles.extend(itertools.repeat(self.itsFocusRole, len(other)))
		else:
			self.itsChars.extend(list(str(other)))
			self.itsStyles.extend(itertools.repeat(self.itsFocusStyle, len(str(other))))
			self.itsRoles.extend(itertools.repeat(self.itsFocusRole, len(str(other))))
		
		return
	

if __name__ == "__main__":
	print("\n\tThis is a Pacify core module and cannot be executed directly.")